﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
    public class ShippedLocationController : ApiController
    {
        // GET api/shippedlocation
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }


        // GET api/shippedlocation/5
        public string Get(string invoice_no)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = @"select invoice_header.invoice_no, invoice_header.target_date, 
                invoice_header.address_text,invoice_header.address_gps, invoice_header.invoice_status, 
                customer.first_name + ' ' + customer.last_name as customer_name
                from invoice_header join customer on 
                invoice_header.customer_id = customer.customer_id 
                where invoice_no = @invoice_no";
                thisCommand.Parameters.AddWithValue("@invoice_no", invoice_no);
                using (SqlDataReader reader = thisCommand.ExecuteReader())
                {
                    List<ShippedLocation> ShippedLocation = new List<ShippedLocation>();
                    while (reader.Read())
                    {
                        string _invoice_no = reader["invoice_no"].ToString();
                        string _target_date = reader["target_date"].ToString();
                        string _address_text = reader["address_text"].ToString();
                        string _address_gps = reader["address_gps"].ToString();
                        string _invoice_status = reader["invoice_status"].ToString();
                        string _customer_name = reader["customer_name"].ToString();
                        ShippedLocation.Add(new ShippedLocation
                        {
                            InvoiceNo = _invoice_no,
                            TargetDate = _target_date,
                            Address = _address_text,
                            AddressGPS = _address_gps,
                            InvoiceStatus = _invoice_status,
                            CustomerName = _customer_name
                        });
                    }
                    thisConnection.Close();
                    string json = JsonConvert.SerializeObject(new
                    {
                        ShippedLocation = ShippedLocation
                    });
                    return json;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // POST api/shippedlocation
        // update address_gps
        public string Post(string invoice_no, string address_gps)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = @"update invoice_header set 
                address_gps = @address_gps 
                where invoice_no = @invoice_no";
                thisCommand.Parameters.AddWithValue("@invoice_no", invoice_no);
                thisCommand.Parameters.AddWithValue("@address_gps", address_gps);
                if (thisCommand.ExecuteNonQuery() > 0)
                {
                    thisConnection.Close();
                    return "true";
                }
                else
                {
                    thisConnection.Close();
                    return "false";
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // PUT api/shippedlocation/5
        public void Put(int id, [FromBody]string value)
        {

        }

        // DELETE api/shippedlocation/5
        public void Delete(int id)
        {
        }
    }
}
